package com.zlsy.learning.leetcode.stack.s496;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author zhouliang
 * @date 2020/8/12 15:14
 *     <p>栈+hashMap 时间复杂度： 空间复杂度：
 */
public class NextGreaterElement1 {
  public static int[] nextGreaterElement(int[] findNums, int[] nums) {
    Stack<Integer> stack = new Stack<>();
    HashMap<Integer, Integer> map = new HashMap<>();
    int[] res = new int[findNums.length];
    for (int i = 0; i < nums.length; i++) {
      while (!stack.empty() && nums[i] > stack.peek()) {
        map.put(stack.pop(), nums[i]);
      }
      stack.push(nums[i]);
    }
    while (!stack.empty()) {
      map.put(stack.pop(), -1);
    }
    for (int i = 0; i < findNums.length; i++) {
      res[i] = map.get(findNums[i]);
    }
    return res;
  }

  public static void main(String[] args) {
    //      nums1 = [4,1,2], nums2 = [1,3,4,2];
    int[] nums1 = new int[] {4, 1, 2};
    int[] nums2 = new int[] {1, 3, 4, 2};
    int[] ints = nextGreaterElement(nums1, nums2);
    System.out.println(ints);
  }
}
